<template>
  <div class="users">
    <div class="operation">
      <el-input
        placeholder="请输入想搜索的用户信息"
        v-model="pages.username"
        clearable
      ></el-input>
      <el-button type="primary" @click="searchUser">搜索</el-button>
      <el-button type="primary" @click="openCreate">添加用户</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="username" label="用户姓名"> </el-table-column>
      <el-table-column prop="role_type" label="用户类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.role_type === '0'" type="success"
            >管理员</el-tag
          >
          <el-tag v-else>普通用户</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openDel(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 创建用户dialog -->
    <el-dialog
      title="新增用户"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" prop="role_type">
          <el-radio-group v-model="form.role_type">
            <el-radio label="0">管理员</el-radio>
            <el-radio label="1">普通用户</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="createUser">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      class="pagination"
      background
      layout="prev, pager, next"
      :page-size="10"
      :total="pages.total"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getUserList, createUser, delUser } from "@/api/modules/webUser";
export default {
  name: "",
  data() {
    return {
      dialogFormVisible: false,
      tableData: [],
      form: {
        username: "",
        password: "",
        role_type: "0",
      },
      pages: {
        username: "",
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
    };
  },
  components: {},
  methods: {
    // 获取用户列表
    getList() {
      getUserList(this.pages).then((res) => {
        const { total, list } = res.data;
        this.tableData = list;
        this.pages.total = total;
      });
    },
    resetForm() {
      this.form = {
        username: "",
        password: "",
        role_type: "1",
      };
    },
    handleCurrentChange(val) {
      this.pages.pageNum = val;
      this.getList();
    },

    // 打开创建dialog
    openCreate() {
      this.dialogFormVisible = true;
      this.resetForm();
    },

    // 搜索用户
    searchUser() {
      this.getList();
    },

    // 创建用户
    createUser() {
      console.log(this.form);
      this.dialogFormVisible = false;
      if (this.form.username === "" || this.form.password === "") {
        this.$message.error("用户名或密码不能为空");
        return;
      }
      createUser(this.form)
        .then((res) => {
          if (res.code === "10000") {
            this.$message({
              message: "创建成功",
              type: "success",
            });
            this.getList();
            this.resetForm();
          }
        })
        .catch(() => {});
    },
    openDel(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.web_id }).then((res) => {
            if (res.code === "10000") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
.users {
  padding: 20px;

  .operation {
    display: flex;
    justify-content: right;
    margin-bottom: 20px;
    .el-input {
      width: 260px;
    }
    .el-button {
      width: 100px;
    }
  }
}
.pagination {
  margin-top: 20px;
  text-align: right;
  padding: 10px;
}
</style>
